﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ADNColorPicker
{
	public partial class FontEditor : UserControl
	{
        private string fontName = "Arial";
        private float fontSize = 14.0f;
        private FontStyle style = FontStyle.Regular;
        private Bitmap preview = new Bitmap(1, 1);

        public FontEditor(string fontName, float fontSize, FontStyle style, string text)
        {
            this.fontName = fontName;
            this.fontSize = fontSize;
            this.style = style;

            rtextText.Text = text;

            cmboFamily.Text = fontName;
            cmboSize.Text = fontSize.ToString();
            CheckStyles();

            InitializeComponent();
            LoadFonts();
            GeneratePreview();
        }

		public FontEditor()
		{
            InitializeComponent();
            LoadFonts();
            GeneratePreview();
		}

        public string FontName
        {
            get { return fontName; }
            set 
            { 
                fontName = value;
                cmboFamily.Text = fontName;
                GeneratePreview();
                pnlPreview.Invalidate();
            }  //TODO: Validate this.
        }

        public float FontSize
        {
            get { return fontSize; }
            set 
            { 
                fontSize = value;
                cmboSize.Text = fontSize.ToString();
                GeneratePreview();
                pnlPreview.Invalidate();
            }
        }

        public FontStyle Style
        {
            get { return style; }
            set 
            { 
                style = value;
                CheckStyles();
                GeneratePreview();
                pnlPreview.Invalidate();
            }
        }

        public string FontText
        {
            get { return rtextText.Text; }
            set 
            { 
                rtextText.Text = value;
                GeneratePreview();
                pnlPreview.Invalidate();
            }
        }

        private void CheckStyles()
        {
            if ((style & FontStyle.Bold) == FontStyle.Bold) { btnBold.Checked = true; }
            if ((style & FontStyle.Italic) == FontStyle.Italic) { btnItalic.Checked = true; }
            if ((style & FontStyle.Underline) == FontStyle.Underline) { btnUnderline.Checked = true; }
        }

        private void LoadFonts()
        {
            FontFamily[] families = FontFamily.Families;

            for (int i = 0; i < families.Length; i++)
            {
                cmboFamily.Items.Add(families[i].Name);
            }
        }

        private void cmboFamily_SelectedIndexChanged(object sender, EventArgs e)
        {
            fontName = cmboFamily.Text;
            GeneratePreview();
            pnlPreview.Invalidate();
        }

        //private void cmboSize_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    fontSize = (float)Double.Parse(cmboSize.Text);
        //    GeneratePreview();
        //    picPreview.Invalidate();
        //}

        private void cmboSize_TextChanged(object sender, EventArgs e)
        {
            fontSize = (float)Double.Parse(cmboSize.Text);
            GeneratePreview();
            pnlPreview.Invalidate();
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            if (btnBold.Checked)
            {
                style = style & ~FontStyle.Regular;
                style = style | FontStyle.Bold;
            }
            else
            {
                style = style & ~FontStyle.Bold;
                style = style | FontStyle.Regular;
            }

            GeneratePreview();
            pnlPreview.Invalidate();
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            if (btnItalic.Checked)
            {
                style = style | FontStyle.Italic;
            }
            else
            {
                style = style & ~FontStyle.Italic;
            }

            GeneratePreview();
            pnlPreview.Invalidate();
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            if (btnUnderline.Checked)
            {
                style = style | FontStyle.Underline;
            }
            else
            {
                style = style & ~FontStyle.Underline;
            }

            GeneratePreview();
            pnlPreview.Invalidate();
        }

        private void rtextText_TextChanged(object sender, EventArgs e)
        {
            GeneratePreview();
            pnlPreview.Invalidate();
        }

        private void GeneratePreview()
        {
            Graphics g = Graphics.FromImage(preview);
            g.Clear(Color.White);
            Font f = new Font(fontName, fontSize, style); //TODO: fix font style
            SizeF s = g.MeasureString(rtextText.Text, f);
            int x = Math.Max(pnlPreview.Width / 2 - (int)s.Width / 2, 0);
            int y = Math.Max(pnlPreview.Height / 2 - (int)s.Height / 2, 0);

            g.DrawString(rtextText.Text, f, new SolidBrush(Color.Black), new PointF(x, y));
            g.Dispose();

        }

        private void pnlPreview_Resize(object sender, EventArgs e)
        {
            if (preview != null)
            {
                preview.Dispose();
                pnlPreview.BackgroundImage = null;
                preview = null;
            }

            preview = new Bitmap(pnlPreview.Width, pnlPreview.Height);
            GeneratePreview();
            pnlPreview.BackgroundImage = preview;
            
        }

    }
}
